package com.neusoft.EYSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.EYSystem.dao.ClientDAO;
import com.neusoft.EYSystem.po.*;
import com.neusoft.EYSystem.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private ClientService clientService;

    @Autowired
    private ClientDAO clientDAO;

    // 管理员端：护理管理客户护理设置 新建按钮先弹出客户表
    @GetMapping("/list")
    public PageResponseBean<List<Client>> listClients(Client client, Long page, Long size) {
        return clientService.listClients(client, page, size);
    }

    @GetMapping("/getClient")
    public ResponseBean<Client> getClient(Long clientId) {
        ResponseBean<Client> response = new ResponseBean<>();
        try {
            Client client = clientDAO.selectById(clientId);
            if (client != null) {
                response.setStatus(200);
                response.setMsg("客户信息查询成功");
                response.setData(client);
            } else {
                response.setStatus(404);
                response.setMsg("客户信息不存在");
            }
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("客户信息查询失败: " + e.getMessage());
        }
        return response;
    }

    @GetMapping("/liveCountHistory")
    public ResponseBean<Map<String, Integer>> getLiveCountHistory() {
        ResponseBean<Map<String, Integer>> response = new ResponseBean<>();
        try {
            Map<String, Integer> count = new HashMap<>();
            QueryWrapper<Client> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status", "active");
            // 获取7天内总人数
            String toDay = java.time.LocalDate.now().toString();
            for (int i = 0; i < 7; i++) {
                String date = java.time.LocalDate.now().minusDays(i).toString();
                queryWrapper.le("checkin_date", date);
                List<Client> clients = clientDAO.selectList(queryWrapper);
                count.put(date, clients.size());
            }
            response.setStatus(200);
            response.setMsg("入住人数统计查询成功");
            response.setData(count);
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("入住人数统计查询失败: " + e.getMessage());
        }
        return response;
    }

    @PostMapping("/checkin")
    public ResponseBean<Integer> addClient(@RequestBody ClientAndMeal clientAndMeal) {
        return clientService.addClient(clientAndMeal);
    }

    @PostMapping("/update")
    public ResponseBean<String> updateClient(Client client) {
        ResponseBean<String> response = new ResponseBean<>();
        try {
            clientDAO.updateById(client);
            response.setStatus(200);
            response.setMsg("客户信息更新成功");
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("客户信息更新失败: " + e.getMessage());
        }
        return response;
    }

    public ResponseBean<Integer> deleteClient(Long clientId) {
        return clientService.logicDeleteClient(clientId);
    }

    // 管理员端：护理管理客户护理设置 新建按钮先弹出客户表 要求弹出无配置过护理等级的客户表

    @GetMapping("/pageClientWithoutNursingLevel")
    public PageResponseBean<List<Client>> pageClientWithoutNursingLevel(Long cur, Long number,Client client) {
        return clientService.pageClientWithoutNursingLevel(cur, number, client);
    }

    //管理员端 护理管理 护理记录 显示客户表
    //重推
    @GetMapping("/pageClient")
    public PageResponseBean<List<Client>> pageClient(Long cur, Long number,Client client) {
        return clientService.pageClient(cur, number, client);
    }

    //管理员端 护理管理 护理记录 显示客户表及其健康管家
    //重推
    @GetMapping("/pageClientAndCaretaker")
    public PageResponseBean<List<ClientAndUser>> pageClientAndCaretaker(Long cur, Long number,Client client) {
        return clientService.pageClientAndCaretaker(cur, number, client);
    }

    @GetMapping("/getClientByUserId")
    public ResponseBean<List<Client>> getClientByUserId(Integer userId) {
        QueryWrapper<Client> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        if (userId == null) {
            return new ResponseBean<>(400, "用户ID不能为空");
        }
        return new ResponseBean<>(200, "查询成功", clientDAO.selectList(queryWrapper));
    }
}
